package teamproject.graphEditor.graphPainters;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import teamproject.graphEditor.Controller;
import teamproject.graphEditor.Node;
import teamproject.graphEditor.PDEEdge;
import teamproject.graphEditor.SortCollectionsToArray;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class BlockNetworkPainter implements GraphPainter {

	private Controller control;
	private int dimension;

	public BlockNetworkPainter(Controller control, int dimension) {
		this.control = control;
		this.dimension = dimension;
	}

	@Override
	public void paintGraph(DirectedGraph<Node, PDEEdge> g) {

		VisualizationViewer<Node, PDEEdge> vv = control.getGraphEditor()
				.getVisualizationViewer();
		vv.getGraphLayout().setGraph(g);

		int maxX = (int) (vv.getSize().width - (vv.getSize().width * 0.2));
		int maxY = (int) (vv.getSize().height - (vv.getSize().height * 0.2));
		int x = 0;
		int y = 0;

		Point2D point = new Point();
		SortCollectionsToArray sca = new SortCollectionsToArray();
		ArrayList<Node> nodes = sca.sortNodes(g.getVertices());

		for (int i = 0; i < nodes.size(); i++) {
			Node n = nodes.get(i);
			int xInterval = maxX / (dimension + 2);
			int yInterval = maxY / dimension;

			if (i % (dimension + 2) != 0)
				x = x + xInterval;
			else {
				y = y + yInterval;
				x = (int) (vv.getSize().width * 0.1);
			}

			point.setLocation(x, y);
			vv.getGraphLayout().setLocation(n, point);
		}

		vv.repaint();
	}
}
